/*
 * Copyright (C) 2018 C-SKY Microsystems Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef AOS_MAILBOX_H
#define AOS_MAILBOX_H

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include <aos/list.h>

/** event ID */
#define CHANNEL_WRITE_EVENT (0x01)
#define CHANNEL_READ_EVENT  (0x02)

typedef struct channel     channel_t;

typedef void (*channel_cb)(int event, void *priv);

/**
 * @brief data struction of channel
*/
struct channel {
    slist_t    next;

    channel_cb cb;      /** callback funtion */
    void      *priv;    /** private data */
    uint32_t   des_id;  /** destination CPU ID */
    void      *context; /** context */
};

/**
 * @brief send message to channel
 * @param [in] ch  : channel handles
 * @param [in] msg : message address
 * @param [in] len : data length
 * @param [in] timeout_ms :  -1 : forever 0: no wait
 * @return 0 on success, -1 on failed
 */
int channel_put_message(channel_t *ch, void *msg, int len, uint32_t timeout_ms);

/**
 * @brief send message to channel
 * @param [in] ch   : channel handles
 * @param [out] msg : message address
 * @param [in] len  : data length
 * @param [in] timeout_ms :  -1 : forever 0: no wait
 * @return 0 on success, -1 on failed
 */
int channel_get_message(channel_t *ch, void *msg, int len, uint32_t timeout_ms);

/**
 * @brief get vaild mailbox channel
 * @param [in] cpu_id : CPU ID
 * @param [in] cb     : callback funtion
 * @param [in] priv   : private data 
 * @return channel handles
 */
channel_t *channel_mailbox_get(int cpu_id, channel_cb cb, void *priv);

#endif // AOS_IPC_H
